package com.alibaba.nacos.test.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;

public class ClientGetConfig {
    public static void main(String[] args) throws NacosException, InterruptedException, IOException {
        String serverAddr = "127.0.0.1";
        String dataId = "nacos-provider-dev.properties";
        String group = "TEST_GROUP";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        // 获取 configService
        ConfigService configService = NacosFactory.createConfigService(properties);
        // 通过 configService 从服务端获取配置信息
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println("第一次接收：" + content);

        // 通过 configService 添加监听，当配置发生变化，客户端会收到通知
        configService.addListener(dataId, group, new Listener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("监听接收：" + configInfo);
            }

            @Override
            public Executor getExecutor() {
                return null;
            }
        });

        int read = System.in.read();
    }
}
